package com.zcp.month;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
 * Created by Administrator on 2017/2/28.
 */

public class CircleView extends View {

    Context context;
    private float intR;
    private float outR;
    private int textSize;
    private String textContent;
    private int ringColor;
    private int sringColor;
    private Paint mPaint;
    private Rect mbounds;

    public CircleView(Context context) {
        this(context, null);
        this.context = context;
    }

    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        intR = typedArray.getDimension(R.styleable.MyView_intR, 40);
        outR = typedArray.getDimension(R.styleable.MyView_outR, 80);
        textSize = typedArray.getDimensionPixelSize(R.styleable.MyView_textSize, 18);
        textContent = typedArray.getString(R.styleable.MyView_textContent);
        ringColor = typedArray.getColor(R.styleable.MyView_ringColor, Color.WHITE);
        sringColor = typedArray.getColor(R.styleable.MyView_SringColor, Color.YELLOW);

        typedArray.recycle();
        //画笔
        mPaint = new Paint();

        //文本范围
        mbounds = new Rect();

        //消除锯齿
        mPaint.setAntiAlias(true);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //矩形
        mPaint.setColor(Color.GREEN);
        canvas.drawRect(getWidth() / 2 - 100, getHeight() / 2 - 100, getWidth() / 2 + 100, getHeight() / 2 + 100, mPaint);
        mPaint.setColor(Color.BLACK);


        mPaint.setStrokeWidth(outR - intR);
        mPaint.setColor(Color.YELLOW);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, outR, mPaint);
        Paint pp = new Paint();
        pp.setColor(Color.WHITE);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, 60, pp);

        //获取文本范围
        Paint paint = new Paint();
        paint.getTextBounds(textContent, 0, textContent.length(), mbounds);
        paint.setTextSize(textSize);
        canvas.drawText(textContent, getWidth() / 2 - mbounds.width() / 2, getHeight() / 2 + mbounds.height() / 2, paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                float x = event.getX();
                float y = event.getY();
                float v = getWidth() / 2 - x;
                float v2 = getHeight() / 2 - y;

                Log.d("zzz", "x" + x);
                float z = v * v + v2 * v2;
                Log.d("zzz", "y" + y);
                double v1 = Math.sqrt(z);
                if (v1 <= 60) {
                    Toast.makeText(context, "在小圆内", Toast.LENGTH_SHORT).show();
                } else if (v1 > 60 && v1 <= 100) {
                    Toast.makeText(context, "在圆环内", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "在圆环外", Toast.LENGTH_SHORT).show();
                }
                break;
        }
        return true;

    }
}
